﻿using System;
using System.Configuration;
using System.Globalization;
using System.Windows.Forms;
using Campus.Properties;
using dbcntn;
using wkwxml;

namespace Campus
{
    public partial class CreateDb : Form
    {
        private string pathXml=String.Empty;
        public CreateDb()
        {
            MaximizeBox = false;
            MinimizeBox = false;
            InitializeComponent();
            pathXml = Application.StartupPath + "\\Connection.xml";//ConfigurationSettings.AppSettings["PathXMLData"].ToString(CultureInfo.InvariantCulture);

        }
        private bool CheckConnection(string aConnectionString)
        {
            _aConnectionToDb = new ConnectionToDb(aConnectionString);
            return _aConnectionToDb.TestConnection();
        }

        private ConnectionToDb _aConnectionToDb;

        private string _connectionstring = "";
        //private const string ConnectionString = "server=localhost;User Id=root;Persist Security Info=True;database=yurt_dbo;password=123456";
        private void Button1Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length==0&&textBox2.Text.Length==0&&textBox3.Text.Length==0&&textBox4.Text.Length==0&&textBox5.Text.Length>0)
            {
                MessageBox.Show(Resources.CreateDb_Button1Click_Баардык_талааларды_толтургула, Resources.CreateDb_Button1Click_Кулактандыруу);
            }
            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0&&textBox5.Text.Length>0)
            {
                Connection a=new Connection(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text,textBox5.Text);
                DataLayer.UpdateConfiguration(DataLayer.GetCurrentConfiguration()[0].ToString(), DataLayer.GetCurrentConfiguration()[1].ToString(), a);
                _connectionstring = DataLayer.GetConfiguration();
                if (CheckConnection(_connectionstring))
                {
                    MessageBox.Show(Resources.CreateDb_Button1Click_Connection_succesfull);
                    Close();
                }
                else
                {
                    MessageBox.Show(Resources.CreateDb_Button1Click_Connection_failed);    
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox4.Clear();
                    textBox5.Clear();
                }
                
            }
        }

        private void CreateDbFormClosed(object sender, FormClosedEventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
        }

        private void CreateDbLoad(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
